//**************************************************************************
// Copyright 2008 Peter Project (http://www.codeplex.com/peter)
//
// License : GNU General Public License (GPL)
// For full text of the license please goto:
// http://www.gnu.org/licenses/gpl-2.0.html
//
// This code is provided on an AS IS basis, with no WARRANTIES,
// CONDITIONS or GUARANTEES of any kind.
//
//		File: InternetBrowser.cs
//
//**************************************************************************

using System;
using System.Drawing;
using System.Windows.Forms;
using PeterInterface;
using WeifenLuo.WinFormsUI.Docking;

namespace InternetBrowser
{
    ///<summary>Plugin wrapper around ctrlInternetBrowser</summary>
    [PeterPlugin(PeterPluginType.DockWindow)]
    public class InternetBrowser : IPeterPlugin
    {
        private IPeterPluginHost m_Host;
        private IDockContent m_ActiveTab;

        ///<summary>Default ctor - defaults ActiveTab to null</summary>
        public InternetBrowser()
        {
            this.m_ActiveTab = null;
        }

        #region IPeterPlugin Members

        public void Start()
        {
        }

        public void Close()
        {
        }

        public void ApplyOptions()
        {
        }

        public string Name
        {
            get { return "Internet ctrlFileBrowser"; }
        }

        public bool HasMenu
        {
            get { return true; }
        }

        public bool HasTabMenu
        {
            get { return true; }
        }

        public bool HasContextMenu
        {
            get { return true; }
        }

        public string Author
        {
            get { return "Jonathan Montiverdi"; }
        }

        public string Version
        {
            get { return "1.0"; }
        }

        public Image PluginImage
        {
            get
            {
                System.Resources.ResourceManager mngr =
                    new System.Resources.ResourceManager("InternetBrowser.InternalImages", this.GetType().Assembly);
                return (Image)mngr.GetObject("Icon");
            }
        }

        public ToolStripMenuItem GetMenu()
        {
            ToolStripMenuItem tsmi = new ToolStripMenuItem("Internet ctrlFileBrowser");
            tsmi.Image = this.PluginImage;
            tsmi.Click += new EventHandler(CreateBrowser);
            return tsmi;
        }

        void CreateBrowser(object sender, EventArgs e)
        {
            ctrlInternetBrowser browser = new ctrlInternetBrowser();
            browser.Icon = Icon.FromHandle(((Bitmap)this.PluginImage).GetHicon());
            browser.Host = this.m_Host;
            this.m_Host.AddDockContent(browser, DockState.Document);
        }

        public ToolStripMenuItem[] GetTabMenu()
        {
            ToolStripMenuItem [] menu = new ToolStripMenuItem[1];
            menu[0] = new ToolStripMenuItem("View in ctrlFileBrowser");
            menu[0].ShortcutKeys = Keys.F5;
            menu[0].Image = this.PluginImage;
            menu[0].Click += new EventHandler(ViewInBrowser);
            return menu;
        }

        void ViewInBrowser(object sender, EventArgs e)
        {
            if (this.m_ActiveTab != null)
            {
                ctrlInternetBrowser browser = new ctrlInternetBrowser();
                browser.Icon = Icon.FromHandle(((Bitmap)this.PluginImage).GetHicon());
                browser.Host = this.m_Host;
                browser.Navigate(((IPeterPluginTab)this.m_ActiveTab).FileName);
                this.m_Host.AddDockContent(browser, DockState.Document);
            }
        }

        public ToolStripMenuItem[] GetContextMenu()
        {
            ToolStripMenuItem[] menu = new ToolStripMenuItem[2];
            menu[0] = new ToolStripMenuItem("View in ctrlFileBrowser");
            menu[0].ShortcutKeys = Keys.F5;
            menu[0].Image = this.PluginImage;
            menu[0].Click += new EventHandler(ViewInBrowser);
            menu[1] = new ToolStripMenuItem("Search with Google");
            System.Resources.ResourceManager mngr =
                new System.Resources.ResourceManager("InternetBrowser.InternalImages", this.GetType().Assembly);
            menu[1].Image = (Image)mngr.GetObject("google_icon");
            menu[1].Click += new EventHandler(SearchGoogle);
            return menu;
        }

        void SearchGoogle(object sender, EventArgs e)
        {
            if (this.m_ActiveTab != null)
            {
                if (((IPeterPluginTab)this.m_ActiveTab).Selection.Length > 0)
                {
                    ctrlInternetBrowser browser = new ctrlInternetBrowser();
                    browser.Icon = Icon.FromHandle(((Bitmap)this.PluginImage).GetHicon());
                    browser.Search(((IPeterPluginTab)this.m_ActiveTab).Selection);
                    browser.Host = this.m_Host;
                    this.m_Host.AddDockContent(browser, DockState.Document);
                }
            }
        }

        public PeterPluginType Type
        {
            get { return PeterPluginType.DockWindow; }
        }

        public IPeterPluginHost Host
        {
            get { return this.m_Host; }

            set { this.m_Host = value; }
        }

        public void ActiveContentChanged(IDockContent tab)
        {
            if (tab != null)
            {
                Type[] types = tab.GetType().GetInterfaces();
                foreach (Type t in types)
                {
                    if (t == typeof(IPeterPluginTab))
                    {
                        this.m_ActiveTab = tab;
                        return;
                    }
                }
                this.m_ActiveTab = null;
            }
        }

        public bool CheckContentString(string contentString)
        {
            if (contentString.Contains("|"))
            {
                string[] splt = contentString.Split('|');
                if (splt[0] == typeof(ctrlInternetBrowser).ToString())
                {
                    return true;
                }
            }
            return false;
        }

        public WeifenLuo.WinFormsUI.Docking.IDockContent GetContent(string contentString)
        {
            ctrlInternetBrowser browser = new ctrlInternetBrowser();
            browser.Icon = Icon.FromHandle(((Bitmap)this.PluginImage).GetHicon());
            string[] splt = contentString.Split('|');
            browser.Navigate(splt[1]);
            browser.Host = this.m_Host;
            return browser;
        }

        public bool AbleToLoadFiles
        {
            get { return false; }
        }

        public bool LoadFile(string filePath)
        {
            return false;
        }

        public Control OptionPanel
        {
            get { return null; }
        }

        #endregion
    }
}
